﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Results;
using PH.Model.Commons;
using PH.Logic.Commons;
namespace PH.Web.Management.Controllers
{
    [RouteArea("Files")]
    public class FileApiController : ApiController
    {
        [Route("GetFile")]
        [HttpPost]
        public JsonEntity<FileModel> GetFile(JObject request)
        {
            string errinfo = "no data";
            try
            {
                if (!string.IsNullOrEmpty(request.Value<string>("guid")))
                {
                    var guid = new Guid(request.Value<string>("guid"));
                    var file = FileLogic.GetFile(guid, true);
                    return new JsonEntity<FileModel>(file);
                }
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<FileModel>(null, errinfo, false);
        }

        [Route("GetFileProgressing")]
        [HttpPost]
        public JsonEntity<FileModel> GetFileProgressing(JObject request)
        {
            string errinfo = "no data";
            try
            {
                if (!string.IsNullOrEmpty(request.Value<string>("guid")))
                {
                    var guid = new Guid(request.Value<string>("guid"));
                    var file = FileLogic.GetFileProgressing(guid);
                    return new JsonEntity<FileModel>(file);
                }
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<FileModel>(null, errinfo, false);
        }
    }
}
